<template>
  <div >
<form-head @onSubmit="onSubmit" :is_change="is_change" :isEdit="true"/>
    <el-row>
      <el-col :xs="24" :sm="12" :lg="12">
        <div class="layout-all" >
        <div class="menu_title" >编辑导航：</div>
        <el-form ref="form" :rules="rules" :model="form" label-width="100px">
          <el-form-item label="导航名:" prop="title" >
            <el-input v-model="form.title" placeholder="请输入导航名" style="width: 70%;"/>
          </el-form-item>
          <el-form-item label="导航分类:" prop="cate_id">
            <el-select v-model="form.cate_id" :disabled="id != 0"  placeholder="请选择导航分类">
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.title"
        
                :value="item.id"
              />
            </el-select>

          </el-form-item>
          <el-form-item label="跳转地址:" prop="url" >
            <el-input v-model="form.url" placeholder="请输入跳转地址" style="width: 70%;"/>
          </el-form-item>
          <el-form-item label="封面:" prop="thumb">
            <upload-pic v-show="this.form.cate_id!=1" :thumb="form.thumb" @update:thumb="form.thumb=$event" width="375" height="100" cate_id="1"/>
            <upload-pic v-show="this.form.cate_id==1" :thumb="form.thumb" @update:thumb="form.thumb=$event" width="100" height="100" cate_id="1"/>
            <div style="font-size:12px;">建议尺寸：首页导航（40X40）广告导航（750X200）</div>
          </el-form-item>
          <el-form-item label="排序:" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" type="number" style="width: 30%;"/>
          </el-form-item>
          <el-form-item label="是否启用:" prop="status">
            <el-switch
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
            />
          </el-form-item>
        </el-form>
        </div>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="12">
        <div class="menu_title">选择跳转地址:</div>
        <url-address @selectMenu="selectMenu" @selectActivity="selectActivity"></url-address>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import urlAddress from '@/components/url_address/urlAddress'
export default {
  components: { urlAddress },
  name:"NavForm",
  computed:{
    id(){
      return this.$store.state.user.editId
    },
    edit(){
      return this.$store.state.user.edit
    },
  },
  data() {
    return {
      menu_list:[],
      is_change: undefined,
      options: [],
      url: '',
      thumb: '',
      form: {
        id: 0,
        sort: 0,
        status: true,
        title: '',
        cate_id: 1,
        url: '',
        thumb: '',
        shop_id: 1
      },
      rules: {
        title: [
          { required: true, message: '请输入导航名', trigger: 'blur' }
        ],
        cate_id: [
          { required: true, message: '请选择导航分类', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.init_into()
  },
  methods: {

    selectActivity (url) {
      this.form.url = url
    },
    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          console.log(form)
          if(form.thumb.length<8)form.thumb=null
          form.status === true ? form.status = 1 : form.status = 0
          this.get('/admin/Ad/save', form, 'POST').then(e => {this.$store.commit('user/SET_EDIT','')})

        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    selectMenu (item) {
      // item.status === 1
      //   ? (this.form.status = true)
      //   : (this.form.status = false);
        this.thumb = this.thumb ? this.thumb : item.thumb;
        this.form.title = this.form.title ? this.form.title : item.title
        this.form.url = item.path
    },
    // 获取当前数据
    init_into() {

      this.get('/admin/Ad/info?id=' + this.id, 'POST').then(e => {
        this.options = e.ad_cate
        this.menu_list = e.user_list
        if(this.id>0){
          this.form = e.info.tpl
          this.form.status = this.form.status ==1
          this.form.shop_id = e.shop_id
        }
        
        setTimeout(()=>{this.is_change=false},100)
      })
    }
  },
  watch: {
    form:{
      handler (val, oldVal) {
	      if(this.is_change!=undefined)
        	this.is_change=true
      },
      deep:true
    }
  }
}
</script>

<style scoped>
.layout-all {
  margin: 10px;
}
.layout-scroll {
    width: 540px;
    height: 567px;
    padding-right: 24px;
    padding-left: 2px;
    overflow-x: hidden;
    overflow-y: auto;
}
.layout-scroll-views {
  text-align:center;
    width: 515px;
    min-height: 667px;
    padding-bottom: 40px;
    padding-top: 2px;
    background-color: #fff;
}
.layout-scroll-right {
    width: 100%;
    height: 670px;
    padding-right: 24px;
    padding-left: 2px;
    overflow-x: hidden;
    overflow-y: auto;
}
.layout-scrollbar::-webkit-scrollbar {
    width: 8px;
}
.layout-scrollbar::-webkit-scrollbar-thumb {
    border-radius: 4px;
    background-color: #999;
}
.layout-scrollbar::-webkit-scrollbar-track {
    background-color: #ddd;
    box-shadow:inset 0 0 2px #333;
    -webkit-box-shadow: inset 0 0 2px #333;
    border-radius: 4px;
}

.menu_title {
  font-size:20px;font-weight:bold;
  padding:10px 0px;
}
</style>
